﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace Web
{
    public partial class uploadWithJqueryelement : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {            
            if (this.IsPostBack  )
            {
                 HttpFileCollection fc  = this.Request.Files;
                 int count = fc.Count;

                 for (int i = 0; i < count; i++)
                 {
                     //文件名
                     string clientFilename = Path.GetFileName(fc[i].FileName);
                     if (clientFilename.Length==0)
                     {
                         continue;
                     }
                     string subfix = Path.GetExtension(fc[i].FileName);
                     Response.Write("<p>"+subfix+"</p>");

                     //文件长度
                     int size = fc[i].ContentLength;
                     //if (size>1024)
                     //{
                     //    Response.Write("<p>文件太长</p>");
                     //    continue;
                     //}
                     string serverfilename = Request.PhysicalApplicationPath + "\\uploadfiles\\" + clientFilename;

                     Upload(fc[i], serverfilename);
                 }     
               if (count==0)
	{
        Response.Write("没有文件或文件不符合规则");
	}
            }
        }

        public void Upload(HttpPostedFile file,string destFile) 
        {
            FileStream fs = new FileStream(destFile, FileMode.OpenOrCreate);
       
            using (BinaryReader br = new BinaryReader(file.InputStream))
            {
                int buffSize = 1024;
                byte[] buf = new byte[buffSize];
                while (true)
                {
                    int readCount = br.Read(buf, 0, buffSize);
                    if (readCount == 0) break;
                    fs.Write(buf, 0, buf.Length);
                }
            }
            fs.Close();
        }
    }
}